#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;

#endregion

namespace ZO.SmartCore.Windows.Forms.Design
{
    internal class SmartWizardDesigner : SmartParentControlDesigner
    {

        #region Constructors
        public SmartWizardDesigner()
            : base(propertiesToRemove)
        {
        }

        #endregion

        #region Destructor

        #endregion

        #region Fields
        private bool _AllowGrid = true;
        private DesignerVerbCollection verbs;

        private static readonly string[] propertiesToRemove = new string[] {
                "Transparent"
            };
        #endregion

        #region Events

        #endregion

        #region Operators

        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets a value indicating whether a grid should be drawn on the control for this designer.
        /// </summary>
        /// <value></value>
        /// <returns>true if a grid should be drawn on the control in the designer; otherwise, false.</returns>
        protected override bool DrawGrid
        {
            get
            {
                return base.DrawGrid && _AllowGrid;
            }
        }

        /// <summary>
        /// Gets the design-time verbs supported by the component that is associated with the designer.
        /// </summary>
        /// <value></value>
        /// <returns>A <see cref="T:System.ComponentModel.Design.DesignerVerbCollection"></see> of <see cref="T:System.ComponentModel.Design.DesignerVerb"></see> objects, or null if no designer verbs are available. This default implementation always returns null.</returns>
        public override DesignerVerbCollection Verbs
        {
            get
            {
                if (verbs == null)
                {
                    DesignerVerb verb1 = new DesignerVerb("Fit &Parent", new EventHandler(this.HandleFitForm));

                    verbs = new DesignerVerbCollection(new DesignerVerb[] { verb1});
                }

                return verbs;
            }
        }


        /// <summary>
        /// Gets the wizard.
        /// </summary>
        /// <value>The wizard.</value>
        private SmartWizard Wizard
        {
            get
            {

                return (this.Control as SmartWizard);
            }

        }

 

        #endregion

        #region Methods

        /// <summary>
        /// Handles the fit form.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void HandleFitForm(object sender, EventArgs e)
        {
            TypeDescriptor.GetProperties(this.Wizard)["Location"].SetValue(this.Wizard, new Point(0, 0));
            
            Form form = this.Wizard.Parent as Form;
            if (form != null)
            {
                TypeDescriptor.GetProperties(form)["ClientSize"].SetValue(form, this.Wizard.Size);
            }

            TypeDescriptor.GetProperties(this.Wizard)["Dock"].SetValue(this.Wizard, DockStyle.Fill);

        }

        internal void SelectPage(SmartWizardPage page)
        {
            ISelectionService service = (ISelectionService)this.GetService(typeof(ISelectionService));
            TypeDescriptor.GetProperties(this.Wizard)["SelectedPage"].SetValue(this.Wizard, page);
        
            if (this.Wizard.SelectedPage != null)
            {
                service.SetSelectedComponents(new object[] { this.Wizard.SelectedPage }, SelectionTypes.Replace);
            }
        }

 


        /// <summary>
        /// Determines whether this instance can parent the specified control.
        /// </summary>
        /// <param name="control">The control.</param>
        /// <returns>
        /// 	<c>true</c> if this instance can parent the specified control; otherwise, <c>false</c>.
        /// </returns>
        public override bool CanParent(Control control)
        {
            if (control is SmartWizardPage)
            {
                return true;
            }

            return false;
        }


        /// <summary>
        /// Indicates whether the control managed by the specified designer can be a child of the control managed by this designer.
        /// </summary>
        /// <param name="controlDesigner">The designer for the control to test.</param>
        /// <returns>
        /// true if the control managed by the specified designer can be a child of the control managed by this designer; otherwise, false.
        /// </returns>
        public override bool CanParent(ControlDesigner controlDesigner)
        {
            if (controlDesigner is SmartWizardPageDesigner)
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// Called when the control that the designer is managing has painted its surface so the designer can paint any additional adornments on top of the control.
        /// </summary>
        /// <param name="pe">A <see cref="T:System.Windows.Forms.PaintEventArgs"></see> that provides data for the event.</param>
        protected override void OnPaintAdornments(PaintEventArgs pe)
        {
            _AllowGrid = false;

            base.OnPaintAdornments(pe);

            _AllowGrid = true;

        }

        /// <summary>
        /// Indicates whether a mouse click at the specified point should be handled by the control.
        /// </summary>
        /// <param name="point">A <see cref="T:System.Drawing.Point"></see> indicating the position at which the mouse was clicked, in screen coordinates.</param>
        /// <returns>
        /// true if a click at the specified point is to be handled by the control; otherwise, false.
        /// </returns>
        protected override bool GetHitTest(Point point)
        {
            SmartWizard wiz = this.Control as SmartWizard;

            if (wiz.NextOrFinishButton.Enabled &&
                wiz.NextOrFinishButton.ClientRectangle.Contains(wiz.NextOrFinishButton.PointToClient(point)))
            {
                //Next can handle that
                return true;
            }
            if (wiz.BackButton.Enabled &&
                wiz.BackButton.ClientRectangle.Contains(wiz.BackButton.PointToClient(point)))
            {
                //Back can handle that
                return true;
            }
            //Nope not interested
            return false;
        }
        #endregion
    }
}
